const app = getApp()
Page({
    data: {
        loading: true
    },

    async onLoad() {
        await this.judgeLogin()
    },

    async judgeLogin() {
        if (wx.getStorageSync('userInfo')) {
            await app.onLogin()
            await wx.redirectTo({url: '/pages/home/home'})
        } else {
            this.setData({
                loading: false
            })
        }
    },

    async getUserProfile() {
        const res = await wx.getUserProfile({
            desc: '用于完善您的信息'
        }).catch(() => {
            // wx.showToast({
            //     title: '仅用于完善您的信息',
            //     icon: 'none'
            // })
            // throw Error('用户未授权')
        })
        // if (!res) return
        app.globalData.userInfo = res ? res.userInfo : null
        wx.setStorageSync('userInfo', res ? res.userInfo : null)
        await app.onLogin()
        await wx.redirectTo({url: '/pages/guide/guide'})
    },

    onCancel() {
        wx.redirectTo({url: '/pages/guide/guide'})
    }
})
